#coding:utf-8

import sys
import random
import argparse
import os
pwd = os.getcwd()
sys.path.append(pwd)
from config.config import Config
pwd = os.getcwd()
sys.path.append(pwd)
conf = Config()
#home_dir =  conf.get_home_data_dir()
#from train_data import genera_data

#生成机器学习的训练数据

#lstm_crf 数据处理
def train_file_data(dir,train_file,dev_file):
    dir_path = dir
    file_names = eachFile(dir_path)
    for file_name in file_names:
        ran_data = random.randint(0, 5)
        if ran_data == 5 :
            output_file = dev_file
        else:
            output_file = train_file
        genera_data(file_name,output_file)
    print '================训练数据处理完毕！====================='

#数据文件连接
def genera_data(file_name,output_file):
    fiobj = open(file_name, 'r')
    trainobj = open(output_file, 'a')
    linea = fiobj.readlines()
    datas = []
    for line in linea:
        datas.append(line)
        trainobj.writelines(line)
    trainobj.flush()



# 遍历指定目录，显示目录下的所有文件名
def eachFile(filepath):
    pathDir = os.listdir(filepath)
    file_names = []
    for allDir in pathDir:
        child = os.path.join('%s%s' % (filepath, allDir))
        child.decode('gbk')  # .decode('gbk')是解决中文显示乱码问题
        file_names.append(child)
    print '原始文档目录扫描完成'
    return file_names

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--home_dir_', dest='home_dir_', type=str, default='/home/yzfu/nlp/kg_abc/abc_project_data/',
                        help='path to the root dir')
    parser.add_argument('--train_path_', dest='train_path_', type=str, default='abc.train',
                        help='path to the training data')
    parser.add_argument('--dev_path_', dest='dev_path_', type=str, default='abc.dev',
                        help='path to the development data')
    parser.add_argument('--product_data_dir_', dest='product_data_dir_', type=str, default='product_03',
                        help='path to the producted data')
    args = parser.parse_args()

    params = vars(args)
    home_dir = params['home_dir_']

    train_path = home_dir+params['train_path_']
    dev_path = home_dir+params['dev_path_']
    product_data_dir = home_dir+params['product_data_dir_']
    train_file_data(product_data_dir, train_path, dev_path)



